package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

//定时类定时方法集合，用于租约管理，定期检查租约是否还有效,若是过期则改为已过期
@Component
public class MyTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //定义定时任务触发的具体方法
    @Scheduled(cron = "0 0 3 * * *")
    public void checkLeaseStatus(){
        //修改租约表状态
        //条件：1.有效租约：257   2.租约结束时间<当前时间（租约已过期），将状态改为过期
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        lambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(lambdaUpdateWrapper);

    }
}
